#include <jni.h>
#include <string>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>

#include "render/EglHelper.h"
#include <android/log.h>
#include <stdint.h>
#include "android/native_window.h"
#include "android/native_window_jni.h"
#include "render/Log.h"
#include "render/BLProgram.h"
#include "render/YUVRender.h"
#include <pthread.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include <unistd.h>

#define LOG_TAG "native-lib"

AAssetManager *manager;
YUVRender *render;
EglHelper *_eglHelper;

extern "C" JNIEXPORT jstring JNICALL
Java_com_dgiot_myapplication_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}



extern "C"
JNIEXPORT void JNICALL
Java_com_dgiot_myapplication_EGLHelper_n_1surfaceCreated(JNIEnv *env, jobject thiz,
                                                         jlong native_ptr, jobject surface) {
    // TODO: implement n_surfaceCreated()
    if (native_ptr != 0) {
        EglHelper *eglHelper = reinterpret_cast<EglHelper*>(native_ptr);
        //  ANativeWindow 本地屏幕的表示
        ANativeWindow *nativeWindow = ANativeWindow_fromSurface(env, surface);
        eglHelper->initEgl(nativeWindow);
    }
}
extern "C"
JNIEXPORT void JNICALL
Java_com_dgiot_myapplication_EGLHelper_n_1surfaceChanged(JNIEnv *env, jobject thiz,
                                                         jlong native_ptr, jint width,
                                                         jint height) {
    // TODO: implement n_surfaceChanged()
    if (native_ptr != 0) {
        EglHelper *eglHelper = reinterpret_cast<EglHelper *>(native_ptr);
        YUVRender *render = new YUVRender(width, height);

        AAsset *asset = AAssetManager_open(manager, "", AASSET_MODE_STREAMING);
        eglHelper->makeCurrent();
        render->prepareRender(1280, 720);
        render->resetRenderSize(width, height);
        int fileSize = AAsset_getLength(asset);
        int width = 1280;
        int height = 720;
        //420p
        int readSize = width * height + (width / 2 * height / 2 ) * 2;
        uint8_t  *buffer = new uint8_t[readSize];
        memset(buffer, 0x00, readSize);
        int count = 0;
        while (count < fileSize) {
            LOGD("start render ......");
            count += AAsset_read(asset, buffer, readSize);
            render->render(buffer, width, height);
            eglHelper->swapBuffers();
        }

        delete[] buffer;
        buffer = nullptr;
        AAsset_close(asset);

//        __android_log_print(ANDROID_LOG_ERROR, "native-lib", "{FORMAT, ##__VA_ARGS__}");
//        glViewport(0, 0, width, height);
//
//        glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
//        glClear(GL_COLOR_BUFFER_BIT);
//        EglHelper *eglHelper = reinterpret_cast<EglHelper *>(native_ptr);
//
//        eglHelper->swapBuffers();
    }
}
extern "C"
JNIEXPORT void JNICALL
Java_com_dgiot_myapplication_EGLHelper_n_1surfaceDestroyed(JNIEnv *env, jobject thiz,
                                                           jlong native_ptr) {
    // TODO: implement n_surfaceDestroyed()
    if (native_ptr != 0) {
        EglHelper *eglHelper = reinterpret_cast<EglHelper*>(native_ptr);
        delete eglHelper;
    }

}

extern "C"
JNIEXPORT jlong JNICALL
Java_com_dgiot_myapplication_EGLHelper_n_1nativePtrInit(JNIEnv *env, jobject thiz,
                                                        jobject asset_manager) {
    // TODO: implement n_nativePtrInit()
    EglHelper *eglHelper = new EglHelper();
    manager = AAssetManager_fromJava(env, asset_manager);
    return reinterpret_cast<jlong>(eglHelper);
}